#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import platform
import subprocess
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowTitle('小红书评论回复 V1.0.0')
        self.setGeometry(100, 100, 500, 640)
        label_browser = QLabel('--浏览器设置区域--', parent=self)

        label_browser.setGeometry(20, 15, 120, 20)
        self.fill_browser_settings()
        label_browser = QLabel('--页面设置区域--', parent=self)
        label_browser.setGeometry(20, 205, 120, 20)

        self.fill_spider()

        start_spider_btn = QPushButton('启动回复', self)
        start_spider_btn.setGeometry(200, 550, 100, 40)
        start_spider_btn.clicked.connect(self.start_spider_btn_on_click)

    # 填充浏览器设置
    def fill_browser_settings(self):

        version_browser = QLabel('版本', parent=self)
        version_browser.setGeometry(20, 60, 100, 20)

        self.version_edit = QLineEdit(self)
        self.version_edit.setGeometry(100, 55, 200, 30)
        self.version_edit.setText('129')

        label_browser = QLabel('端口', parent=self)
        label_browser.setGeometry(20, 100, 100, 20)

        self.port_edit = QLineEdit(self)
        self.port_edit.setGeometry(100, 95, 200, 30)
        self.port_edit.setText('9222')

        start_browser_btn = QPushButton('启动浏览器', self)
        start_browser_btn.setGeometry(200, 140, 100, 40)
        start_browser_btn.clicked.connect(self.start_browser_btn_on_click)

    def start_browser(self, port):
        if not port:
            self.show_message("输入回复中包含的关键词")
        else:
            if platform.system() == 'Windows':
                print('start windows brower')
                subprocess.run('C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe --remote-debugging-port={}'.format(port))
            elif platform.system() == 'Darwin':
                subprocess.run('/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port={} &'.format(port), shell=True)

    def start_browser_btn_on_click(self):
        print('start_browser_btn_on_click')
        # self.port_edit.setReadOnly(True)
        port = self.port_edit.text()
        version = self.version_edit.text()
        if not port:
            self.show_message("输入浏览器启动端口")
        elif not version:
            self.show_message("输入浏览器版本")
        else:
            print(port)
            self.start_browser(port)

    # 填充爬取设置
    def fill_spider(self):
        label_url = QLabel('链接', parent=self)
        label_url.setGeometry(20, 250, 100, 20)

        self.url_edit = QLineEdit(self)
        self.url_edit.setGeometry(100, 245, 300, 30)

        label_keywords = QLabel('关键词', parent=self)
        label_keywords.setGeometry(20, 300, 100, 20)

        self.keywords_edit = QTextEdit(self)
        self.keywords_edit.setGeometry(100, 300, 300, 150)

        label_reply_content = QLabel('回复内容', parent=self)
        label_reply_content.setGeometry(20, 480, 100, 20)

        self.reply_content_edit = QTextEdit(self)
        self.reply_content_edit.setGeometry(100, 475, 300, 60)

    def start_spider(self, url, keywords, reply_content, version, port):
        if platform.system() == 'Windows':
            print('start windows spider')
            subprocess.run('start_spider.bat {url} {keywords} {reply_content} {version} {port}'.format(url=url,
                                                                                                          keywords=keywords,
                                                                                                          reply_content=reply_content,
                                                                                                          version=version,
                                                                                                          port=port))
        elif platform.system() == 'Darwin':
            subprocess.run('./start_spider.sh {url} {keywords} {reply_content} {version} {port} &'.format(url=url, keywords=keywords, reply_content=reply_content, version=version, port=port), shell=True)

    def start_spider_btn_on_click(self):
        print('start_spider_btn_on_click')
        url = self.url_edit.text()
        keywords = self.keywords_edit.toPlainText()
        reply_content = self.reply_content_edit.toPlainText()
        port = self.port_edit.text()
        version = self.version_edit.text()
        if not url:
            self.show_message("输入需要回复用户的小红书主页")
        elif not keywords:
            self.show_message("输入回复中包含的关键词")
        elif not keywords:
            self.show_message("输入回复中包含的关键词")
        elif not reply_content:
            self.show_message("输入回复内容")
        elif not port:
            self.show_message("输入浏览器启动端口")
        elif not version:
            self.show_message("输入浏览器版本")
        else:
            keywords = keywords.replace('\n', ',')
            print("start spider")
            self.start_spider(url, keywords, reply_content, version, port)
    def show_message(self, message):
        QMessageBox.critical(self, "错误", message)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())